package org.example.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.example.commmon.R;
import org.example.pojo.Vip;
import org.example.service.VipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@RestController//controller层的注解
@Slf4j



@RequestMapping("/employee")//httt://localhost:8080/employee/....
public class VipController {
    @Autowired
    private VipService vipService;

    @PostMapping("/login")
    public R<Vip> login(HttpServletRequest request, @RequestBody Vip vip){
            if (vip ==null) return R.error("客户端异常");
            return  vipService.getEmployee(request, vip);

    }

    /**
     * 退出登录
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("vip");
        return R.success("退出成功");
    }

    /**
     *使用公共填充
     */
    //新增员工信息
//    @PostMapping  //"/vip"
//    public R<String> save(@RequestBody Vip vip, HttpServletRequest request){
////        MyMetaObjeactHandler完成
//        //前端将基础数据employee传给后端 后端处理更新时间,默认密码等字段
//        vip.setCreateTime(LocalDateTime.now());
//        vip.setUpdateTime(LocalDateTime.now());
//        //对密码进行md5加密
//        String pwd="123456";            //默认会员密码
//        String pwd_MD5 = DigestUtils.md5DigestAsHex(pwd.getBytes());
//        vip.setPassword(pwd_MD5);
//        //获取操作人的信息
//         Long  createUserId= (Long) request.getSession().getAttribute("vip");
//         Long  updateUserId= (Long) request.getSession().getAttribute("vip");
//         vip.setCreateUser(createUserId);
//         vip.setUpdateUser(updateUserId);
////         最后进行mp的调用 将数据保存到数据库
//        vipService.save(vip);
//         return R.success("新增employee成功");
//    }


    @PostMapping  //"/vip"
    public R<String> save(@RequestBody Vip vip, HttpServletRequest request){
        //对密码进行md5加密
        String pwd="123456";            //默认会员密码
        String pwd_MD5 = DigestUtils.md5DigestAsHex(pwd.getBytes());
        vip.setPassword(pwd_MD5);
        //最后进行mp的调用 将数据保存到数据库
        vipService.save(vip);
        return R.success("新增vip成功");
    }

    //员工分页查询
    /**
     * 页面发送ajax请求 将分页查询的page，pagesize，name等提交给服务端
     * 服务端调用分页查询的功能
     * 服务端将数据返回给页面
     * 用到MybatisPlusConfig
     *
     */
    /**
     *
     * @param page 第几页
     * @param pageSize 每页面多少跳数据
     * @param name 页面按照员工姓名查询参数
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name){

        Page page1 = vipService.getPage(page, pageSize, name);
        return R.success(page1);
    }

    //更新

    /**
     * 状态的更新（禁用/启用）
     * 其他信息更新
     * @param vip
     * @return
     */

    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Vip vip){

        //拿到操作者的id
        Long operatorId= (Long) request.getSession().getAttribute("vip");
        //操作更新
        vip.setUpdateTime(LocalDateTime.now());
        vip.setUpdateUser(operatorId);
        vipService.updateById(vip);

        return R.success("修改成功");
    }

    //根据Id查询
    @GetMapping("/{id}")
    public R<Vip> getById(@PathVariable Long id){
        Vip vip = vipService.getById(id);
        return R.success(vip);
    }





}
